define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/pool/index' + location.search,
                    add_url: 'user/pool/add',
                    edit_url: 'user/pool/edit',
                    del_url: 'user/pool/del',
                    multi_url: 'user/pool/multi',
                    import_url: 'user/pool/import',
                    table: 'user_pool',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {
                            checkbox: true, formatter: function (value, row, index) {
                                if (row.status == 0) {
                                    return {
                                        disabled: false
                                    }
                                }else{
                                    return {
                                        disabled: true
                                    }
                                }
                            }
                        },
                        { field: 'id', title: __('Id') },
                        { field: 'name', title: __('Name'), operate: 'LIKE' },
                        { field: 'source', title: __('Source'), operate: 'LIKE' },
                        { field: 'mobile', title: __('Mobile'), operate: 'LIKE' },
                        { field: 'wx', title: __('Wx'), operate: 'LIKE' },
                        { field: 'sex', title: __('Sex'), operate: 'LIKE' },
                        { field: 'mark', title: __('Mark'), operate: 'LIKE' },
                        { field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        { field: 'acct_id', title: __('Acct_id'), searchList: Config.adminlist, formatter: Table.api.formatter.normal },
                        { field: 'status', title: __('Status'), searchList: { "0": __('Status 0'), "1": __('Status 1'), "2": __('Status 2'), "3": __('Status 3') }, formatter: Table.api.formatter.status },
                        { field: 'channelinfo.nickname', title: __('Channel') },
                        { field: 'claiminfo.nickname', title: __('Claim') },
                        { field: 'intention', title: __('意向程度') },
                        { field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate }
                    ]
                ]
            });
            $('#distributionButton').on('click', function (row) {
                var ids = Table.api.selectedids(table);
                Fast.api.open('user/pool/allocation?ids=' + ids.join(","), '批量分配');
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        allocation: function () {
            $("#c-channel").data("format-item", function (row) {
                return row.nickname + "#" + row.id;
            });
            Controller.api.bindevent();
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
